<?php
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/uWorkSku.class.php');

function cart_fetch() {
	$rCart = array();
	if ($_COOKIE['cart']) {
		$rItems = explode(',', trim($_COOKIE['cart'], ', '));
		foreach ($rItems as $sCode) {
			list($nItemID, $nSizeID, $nKuangID, $nQuantity) = explode('_', $sCode);
			$nItemID = intval($nItemID);
			$nSizeID = intval($nSizeID);
			$nKuangID = intval($nKuangID);
			$nQuantity = intval($nQuantity);
			if (0 < $nItemID && 0 < $nQuantity) {
				$rCart[] = array(
					'Code' => $sCode,
					'ItemID' => $nItemID,
					'SizeID' => $nSizeID,
					'KuangID' => $nKuangID,
					'Quantity' => $nQuantity,
				);
			}
		}
	}
	return $rCart;
}

function cart_add($nItemID, $nSizeID, $nKuangID, $nQuantity) {
	if (!(0 < $nItemID)) { return false; }
	$nSizeID = (int)$nSizeID;
	if (!(0 < $nQuantity)) { return false; }
	/// 找到则增加，否则加入 ///
	$bFound = false;
	$rItems = explode(',', trim($_COOKIE['cart'], ', '));
	foreach ($rItems as $k => $sCode) {
		list($nI, $nS, $nK, $nQ) = explode('_', $sCode);
		if ($nI == $nItemID && $nS == $nSizeID && $nK == $nKuangID) {
			$nQ += $nQuantity; // 增加
			$oWork = new uWork($nI);
			$rStock = $oWork->getSku($nS, $nK);
			if ($rStock['stock'] < $nQ) { return false; } // 库存不够
			$rItems[$k] = "{$nI}_{$nS}_{$nK}_{$nQ}";
			$bFound = true;
			break;
		}
	}
	if (!$bFound) { $rItems[] = "{$nItemID}_{$nSizeID}_{$nKuangID}_{$nQuantity}"; }
	/// 重设cookie ///
	$sCart = implode(',', $rItems);
	setcookie('cart', $sCart, null, '/'); $_COOKIE['cart'] = $sCart;
	return true;
}
function cart_update($nItemID, $nSizeID, $nKuangID, $nQuantity) {
	if (!(0 < $nItemID)) { return false; }
	$nSizeID = (int)$nSizeID;
	if (!(0 < $nQuantity)) { return cart_delete($nItemID, $nSizeID, $nKuangID); }
	/// 找到则改，否则报错 ///
	$bFound = false;
	$rItems = explode(',', trim($_COOKIE['cart'], ', '));
	foreach ($rItems as $k => $sCode) {
		list($nI, $nS, $nK, $nQ) = explode('_', $sCode);
		if ($nI == $nItemID && $nS == $nSizeID && $nK == $nKuangID) {
			$nQ = $nQuantity; // 修改
			$oWork = new uWork($nI);
			$rStock = $oWork->getSku($nS, $nK);
			if ($rStock['stock'] < $nQ) { return false; } // 库存不够
			$rItems[$k] = "{$nI}_{$nS}_{$nK}_{$nQ}";
			$bFound = true;
			break;
		}
	}
	if (!$bFound) { return false; }
	/// 重设cookie ///
	$sCart = implode(',', $rItems);
	setcookie('cart', $sCart, null, '/'); $_COOKIE['cart'] = $sCart;
	return true;
}
function cart_delete($nItemID, $nSizeID, $nKuangID) {
	if (!(0 < $nItemID)) { return false; }
	$nSizeID = (int)$nSizeID;
	/// 找到则删，否则报错 ///
	$bFound = false;
	$rItems = explode(',', trim($_COOKIE['cart'], ', '));
	foreach ($rItems as $k => $sCode) {
		list($nI, $nS, $nK, $nQ) = explode('_', $sCode);
		if ($nI == $nItemID && $nS == $nSizeID && $nK == $nKuangID) {
			unset($rItems[$k]); // 删除
			$bFound = true;
			break;
		}
	}
	if (!$bFound) { return false; }
	/// 重设cookie ///
	$sCart = implode(',', $rItems);
	setcookie('cart', $sCart, null, '/'); $_COOKIE['cart'] = $sCart;
	return true;
}

function cart_delete_all() {
	$sCart = '';
	setcookie('cart', $sCart, null, '/'); $_COOKIE['cart'] = $sCart;
	return true;
}
?>
